package org.example.service;

import org.example.dao.AdminLoginDao;
import org.example.model.Admin;
import org.example.model.Menu;
import org.example.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
// 这个注解表示这是一个服务类，并且在事务中进行操作，如果出现异常，则回滚事务
@Transactional(rollbackFor = Exception.class)
public class AdminLoginService {
    @Autowired
    AdminLoginDao loginDao;



    public Admin login(Admin admin) {
        Admin admin1 =loginDao.login(admin);
        if (admin1 != null) {
//            生成Token
            String adminToken = JWTUtil.getToken(admin1);
            admin1.setAdminToken(adminToken);
        }
        return admin1;
    }

    public List<Menu> getMenus(String adminToken) {
        Admin admin = JWTUtil.getAdmin(adminToken);
        return loginDao.getMenus(admin.getId());
    }
}
